"use strict";

const path = require("path");
const os = require("os");
const fs = require("fs");

const test = require("ava").test;
const workPlus = require("../index");
const idc = workPlus.idc;
const admin = workPlus.admin;
const aq = workPlus.aq();
const resource = workPlus.resource();

const domain = idc.domain();
const app = admin.app();

const fail = function(e, t) {
	console.error(e, e.stack);
	t.fail();
};
const domainInfo = {
	id_name: `test_${Date.now()}`,
	access_endpoint: "https://api.workplus.io",
	website_endpoint: "https://dashboard.workplus.io",
	display_name: "测试域",
	display_intro: "测试域简介",
	max_orgs: 1
};
const nextStep = function(flag, t, returned) {
	if (flag) {
		return returned;
	} else {
		t.fail();
	}
};

const successOrFail = function(flag, t) {
	if (flag) {
		t.pass();
	} else {
		t.fail();
	}
};

const downloadFile = (fileId, dest) => resource.downloadFile(fileId, dest);

const checkUploadResult = function(feedback, t) {
	const flag = feedback.status === 0 && feedback.result.length > 10;
	return nextStep(flag, t, feedback.result);
};

const checkDownloadResult = function(originalFile, dest, t) {
	const originalSize = fs.statSync(originalFile).size;
	const downloadSize = fs.statSync(dest).size;
	const flag = originalSize === downloadSize;
	successOrFail(flag, t);
};


test.serial("create_then_query_then_delete", t => {
	const checkCreateResult = function(feedback) {
		const flag = feedback.status === 0 && feedback.result.id_name === domainInfo.id_name;
		return nextStep(flag, t, feedback.result.id_name);
	};
	const checkQueryResult = function(feedback) {
		const flag = feedback.status === 0 && feedback.result.total_count === 1;
		console.log(feedback.result.records);
		return nextStep(flag, t, feedback.result.records[0].id);
	};
	const checkDeleteReuslt = function(feedback){
		const flag  = feedback.status === 0;
		successOrFail(flag, t);
	};
	return domain.createDomain(domainInfo)
		.then(checkCreateResult)
		.then(domain.queryDomainById)
		.then(checkQueryResult)
		.then(domain.deleteDomainById)
		.then(checkDeleteReuslt)
		.catch(e => fail(e, t));
});

test.serial("test_upload_file_then_download", t => {
	const originalFile = "../LICENSE" ;
	const dest = path.join(os.tmpdir(), "workplus_test.txt");
	const file = {
		path: originalFile
	};
	return resource.uploadFile(file)
		.then(feedback => checkUploadResult(feedback, t))
		.then(fileId => downloadFile(fileId, dest))
		.then(() => checkDownloadResult(originalFile, dest, t))
		.catch(e => fail(e, t));
});

test.serial("test_upload_image", t => {
	const originalFile = "../resource/test.jpg";
	const dest = path.join(os.tmpdir(), path.basename(originalFile));
	const image = {
		path: originalFile
	};
	return resource.uploadImage(image)
		.then(feedback => checkUploadResult(feedback, t))
		.then(fileId => downloadFile(fileId, dest))
		.then(() => checkDownloadResult(originalFile, dest, t))
		.catch(e => fail(e, t));
});
test.serial("test_put_onTo_shelf", t => {
	const param = {
		domainId: "n666666-app",
		downloadLink: "http://res.beeworks.io/v1/Z3JvdXAxL00wMC8wMC81NC9DZ29LTFZodG94U0FGVDZlQVQxX2JVNEs3V1U0MjYuaXBh",
		fileName: "勾手你大爷3测试",
		intro: "勾手你大爷3",
		iconUrl: "http://www.oschina.net/favicon.ico",
		appName: "勾手你大爷3",
		pkgName: "com.foreveross.atwork.dev",
		buildNo: "1",
		appVersion: " param.appVersion",
		platform: "ios"
	};
	return app.putOnToShelf(param)
		.then(() => t.pass())
		.catch(e => fail(e, t));
});

//这个以后不用再跑了，链接就是这个https://aq.beeworks.io/v1/secure-captchas?bits=4&survival_seconds=60&addresser=::1&recipient=13580585282
/*test("test_get_captcha", t => {
	const url = aq.getCaptchaUrl("bits=4&survival_seconds=60&addresser=::1&recipient=13580585282");
	console.log(url);
	t.pass();
});*/

//短信验证码相关
/*const param = require("./captcha_param");
test.serial("test_get_sms", t => {
	return aq.getSmsCode(param)
		.then(() => t.pass())
		.catch(e => fail(e, t));
});
test.only("test_check_sms", t => {
	param.code = "9435";
	return aq.validateSmsCode(param)
		.then(() => t.pass())
		.catch(e => fail(e, t));
});

//图形验证码相关

test.serial("test_check_captcha", t => {
	return aq.validateCaptcha(param)
		.then(() => t.pass())
		.catch(e => fail(e, t));
});*/


